unit clsHist_Maquina;

interface

uses Dm,dbtables,biblio;

type THist_Maquina =class(TObject)
  public
    Descricao: string;
    function  Retorna_Ultimo                 : string;
    function  Recuperar(Cod_HistMaq: string): boolean;
    procedure Gravar   (Cod_HistMaq: string);
    procedure Excluir  (Cod_HistMaq: string);
    procedure Consultar(Desc_Hist: string; var tq:TQuery);
  end;

implementation

procedure THist_Maquina.Consultar;
begin
 tq.close;
 tq.sql.Clear;
 tq.Params.Clear;
 tq.sql.add('Select * from Hist_Maquina where Descricao>=:a order by Descricao');
 tq.params[0].asstring:= Desc_Hist;
 tq.open;
end;

procedure THist_Maquina.Excluir;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Delete from Hist_Maquina where Cod_HistMaq=:a');
 tq.params[0].asstring:= Cod_HistMaq;
 tq.execsql;
 tq.close;
 tq.free;
end;

procedure THist_Maquina.Gravar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Hist_Maquina where Cod_HistMaq=:a');
 tq.params[0].asstring:= Cod_HistMaq;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Insert into Hist_Maquina values(:k_Cod_HistMaq,:k_Descricao)');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Update Hist_Maquina set Descricao=:k_Descricao where '+
             'Cod_HistMaq=:k_Cod_HistMaq');
 end;
 tq.parambyname('k_Cod_HistMaq').asstring:= Cod_HistMaq;
 tq.parambyname('k_Descricao').asstring  := Descricao;
 tq.execsql;
 tq.close;
 tq.free;
end;

function THist_Maquina.Recuperar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Hist_Maquina where Cod_HistMaq=:a');
 tq.params[0].asstring:= Cod_HistMaq;
 tq.open;
 if not tq.eof then result:= true else result:= false;
 Descricao:= tq.fieldbyname('Descricao').asstring;
 tq.close;
 tq.free;
end;

function THist_Maquina.Retorna_Ultimo;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select max(Cod_HistMaq) ultimo from Hist_Maquina');
 tq.open;
 if tq.fieldbyname('ultimo').IsNull then
  result:= '001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,3);
 tq.close;
 tq.free;
end;

end.
